/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterAddress.java
 *
 * Created on Mar 22, 2011, 2:14:13 PM
 */

/**
 *
 * @author Claudia
 */
public class RegisterAddress extends javax.swing.JFrame {

    /** Creates new form RegisterAddress */
    public RegisterAddress() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitleLabel = new javax.swing.JLabel();
        StreetNoLabel = new javax.swing.JLabel();
        AptLabel = new javax.swing.JLabel();
        StreetLabel = new javax.swing.JLabel();
        ProvinceLabel = new javax.swing.JLabel();
        CityLabel = new javax.swing.JLabel();
        PostalLabel = new javax.swing.JLabel();
        StreetNoText = new javax.swing.JTextField();
        AptText = new javax.swing.JTextField();
        StreetText = new javax.swing.JTextField();
        CityText = new javax.swing.JTextField();
        ProvinceSelect = new javax.swing.JComboBox();
        PostalText = new javax.swing.JTextField();
        ConfirmButton = new javax.swing.JButton();
        ClearButton = new javax.swing.JButton();
        StreetOut = new javax.swing.JLabel();
        CityOut = new javax.swing.JLabel();
        PostalOut = new javax.swing.JLabel();
        StreetNoOut = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(50, 30, 800, 600));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(800, 600));

        TitleLabel.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        TitleLabel.setText("Register New Address");

        StreetNoLabel.setText("Street Number");

        AptLabel.setText("Apt Number");

        StreetLabel.setText("Street");

        ProvinceLabel.setText("Province");

        CityLabel.setText("City");

        PostalLabel.setText("Postal Code");

        ProvinceSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon" }));

        ConfirmButton.setText("Add Address");
        ConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmButtonActionPerformed(evt);
            }
        });

        ClearButton.setText("Clear Form");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(ConfirmButton)
                        .addGap(154, 154, 154)
                        .addComponent(ClearButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ProvinceLabel)
                            .addComponent(AptLabel)
                            .addComponent(StreetLabel)
                            .addComponent(CityLabel)
                            .addComponent(StreetNoLabel)
                            .addComponent(PostalLabel))
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(TitleLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(AptText)
                                        .addComponent(StreetNoText)
                                        .addComponent(StreetText)
                                        .addComponent(CityText))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(PostalText, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(ProvinceSelect, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGap(173, 173, 173)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(PostalOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(StreetOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(CityOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(StreetNoOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(106, 106, 106)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(TitleLabel)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(StreetNoLabel)
                            .addComponent(StreetNoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(AptLabel)
                            .addComponent(AptText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(StreetLabel)
                            .addComponent(StreetText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(CityLabel)
                            .addComponent(CityText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PostalLabel)
                            .addComponent(PostalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ProvinceSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ProvinceLabel))
                        .addGap(93, 93, 93)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ConfirmButton)
                            .addComponent(ClearButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(StreetNoOut, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(StreetOut, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(CityOut, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(PostalOut, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(94, 94, 94))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmButtonActionPerformed
        // TODO add your handling code here:

        if (StreetNoText.getText().length() == 0) {
            StreetNoOut.setText("Street Number Field Required");
        } else {
            StreetNoOut.setText("");
        }

        if (PostalText.getText().length() == 0){
            PostalOut.setText("Postal Code Field Required");
        } else {
            ValidatePostal();
            }

        if (CityText.getText().length() == 0) {
            CityOut.setText("City Field Required");
        } else {
            CityOut.setText("");
        }

        if (StreetText.getText().length() == 0){
            StreetOut.setText("Street Field Required");
        } else {
            StreetOut.setText("");
        }


    }//GEN-LAST:event_ConfirmButtonActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        // TODO add your handling code here:
        AptText.setText("");
        CityText.setText("");
        CityOut.setText("");
        PostalText.setText("");
        StreetNoOut.setText("");
        StreetNoText.setText("");
        StreetText.setText("");
        StreetOut.setText("");
        PostalOut.setText("");
    }//GEN-LAST:event_ClearButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RegisterAddress().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AptLabel;
    private javax.swing.JTextField AptText;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JLabel CityOut;
    private javax.swing.JTextField CityText;
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton ConfirmButton;
    private javax.swing.JLabel PostalLabel;
    private javax.swing.JLabel PostalOut;
    private javax.swing.JTextField PostalText;
    private javax.swing.JLabel ProvinceLabel;
    private javax.swing.JComboBox ProvinceSelect;
    private javax.swing.JLabel StreetLabel;
    private javax.swing.JLabel StreetNoLabel;
    private javax.swing.JLabel StreetNoOut;
    private javax.swing.JTextField StreetNoText;
    private javax.swing.JLabel StreetOut;
    private javax.swing.JTextField StreetText;
    private javax.swing.JLabel TitleLabel;
    // End of variables declaration//GEN-END:variables

    private void ValidatePostal() {


        if (PostalText.getText().length() != 6) {
            PostalOut.setText("Less than 6");
        }

        String option = (String)ProvinceSelect.getSelectedItem();

        char [] postal = PostalText.getText().toUpperCase().toCharArray();
        
        int ascii1 = PostalText.getText().charAt(1);
        int ascii2 = PostalText.getText().toUpperCase().charAt(2);
        int ascii3 = PostalText.getText().charAt(3);
        int ascii4 = PostalText.getText().toUpperCase().charAt(4);
        int ascii5 = PostalText.getText().charAt(5);

        if (ascii1 < 48 || ascii1 > 57) { //Is Number
            PostalOut.setText("Postal Code Invalid");
        } else if (ascii2 < 65 || ascii2 > 90) {
            PostalOut.setText("Postal Code Invalid");
        } else if (ascii3 < 48 || ascii3 > 57) {
            PostalOut.setText("Postal Code Invalid");
        } else if (ascii5 < 48 || ascii5 > 57) {
            PostalOut.setText("Postal Code Invalid");
        } else if (ascii4 < 65 || ascii4 > 90) {
            PostalOut.setText("Postal Code Invalid");
        } else {
            PostalOut.setText("");
        }

        if (postal[0] == 'A' || postal[0] == 'B' || postal[0] == 'C' || postal[0] == 'E' || postal[0] == 'G' || postal[0] == 'H' ||
                postal[0] == 'J' || postal[0] == 'K' || postal[0] == 'L' || postal[0] == 'M' || postal[0] == 'N' || postal[0] == 'P' ||
                postal[0] == 'R' || postal[0] == 'S' || postal[0] == 'T' || postal[0] == 'V' || postal[0] == 'X' || postal[0] == 'Y') {
            if (postal[0] == 'A' && option.equals("Newfoundland and Labrador") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'B' && option.equals("Nova Scotia") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'C' && option.equals("Prince Edward Island") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'E' && option.equals("New Brunswick") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'G' && option.equals("Quebec") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'H' && option.equals("Quebec") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'J' && option.equals("Quebec") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'K' && option.equals("Ontario") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'L' && option.equals("Ontario") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'M' && option.equals("Ontario") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'N' && option.equals("Ontario") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'P' && option.equals("Ontario") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'R' && option.equals("Manitoba") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'S' && option.equals("Saskatchewan") == false){
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'T' && option.equals("Alberta") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'V' && option.equals("British Columbia") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'X' && option.equals("Northwest Territories") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal [0] == 'X' && option.equals("Nunavut") == false) {
                PostalOut.setText("Postal Code Invalid");
            } else if (postal[0] == 'Y' && option.equals("Yukon") == false) {
                PostalOut.setText("Postal Code Invalid");
            } 
        } else {
            PostalOut.setText("Postal Code Invalid");
        }
    }
}


